package org.example.lotterysystem.dao.mapper;

import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.*;
import org.example.lotterysystem.dao.dataobject.WinnerRecordDO;

import java.util.List;

@Mapper
public interface WinnerRecordMapper {
    @Insert("<script>" +
            "insert into winning_record" +
            " (activity_id,activity_name," +
            " prize_id,prize_name,prize_tier," +
            " winner_id,winner_name,winner_email,winner_phone_number,winning_time)" +
            " values" +
            " <foreach  collection = 'items' item = 'item' index = 'index' separator = ',' >" +
            "(#{item.activityId},#{item.activityName}," +
            "#{item.prizeId},#{item.prizeName},#{item.prizeTier}," +
            "#{item.winnerId},#{item.winnerName},#{item.winnerEmail},#{item.winnerPhoneNumber},#{item.winningTime})" +
            "</foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
   int batchInsert(@Param("items") List<WinnerRecordDO> winningRecordDoList);

    @Select("select * from winning_record where activity_id = #{activityId}")
    List<WinnerRecordDO> selectByActivityId(@Param("activityId") Long activityId);

    @Select("select count(1) from winning_record where activity_id = #{activityId} and prize_id = #{prizeId}")
    int countByAPId(@Param("activityId") Long activityId,@Param(" prizeId") Long prizeId);

    /**
     * 删除活动或者奖品下的中奖记录
     * @param activityId
     * @param prizeId
     */
    @Delete("<script>" +
            " delete from winning_record" +
            " where activity_id = #{activityId} " +
            " <if test = \"prizeId != null\">" +
            " and prize_id = #{prizeId}</if>" +
            "</script>")
    void deleteRecords(@Param("activityId") Long activityId,@Param("activityId") Long prizeId);

    /**
     * 根据id或者奖品id查询中奖记录
     * @param activityId
     * @param prizeId
     * @return
     */
    @Select("<script>" +
            " select * from winning_record" +
            " where activity_id = #{activityId}" +
            " <if test = \"prizeId != null\">" +
            " and prize_id = #{prizeId}" +
            "</if>" +
            "</script>")
    List<WinnerRecordDO> selectByActivityIdOrPrizeId(@Param("activityId")@NotNull(message = "活动id不能为空") Long activityId, @Param("prizeId")Long prizeId);
}
